package cn.iocoder.yudao.module.bpm.framework.flowable.core.listener.demo.exection;

import lombok.extern.slf4j.Slf4j;
import org.flowable.bpmn.model.ExclusiveGateway;
import org.flowable.bpmn.model.FlowElement;
import org.flowable.bpmn.model.SequenceFlow;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.JavaDelegate;
import org.springframework.stereotype.Component;

/**
 * 类型为 delegateExpression 的 ExecutionListener 监听器示例
 *
 * 和 {@link DemoDelegateClassExecutionListener} 的差异是，需要注册到 Spring 中
 */
@Component
@Slf4j
public class DemoDelegateExpressionExecutionListener implements JavaDelegate {

    /**
     * @Description: 网关流转
     * @Author: 雨同我
     * @DateTime: 2024/8/13 15:31
     * @param: null:
     * @return:
     */

    @Override
    public void execute(DelegateExecution execution) {
        System.out.println("执行监听2024");
        FlowElement currentFlowElement = execution.getCurrentFlowElement();
        ExclusiveGateway gateway = (ExclusiveGateway) currentFlowElement;
        System.out.println("找到网关: " + gateway.getName());
        // 获取网关的所有出线
        for (SequenceFlow flow : gateway.getOutgoingFlows()) {
            System.out.println("  出线ID: " + flow.getId());
            System.out.println("  条件表达式: " + flow.getConditionExpression());
            System.out.println("名字 "+ flow.getName());
            // 如果需要，可以在这里添加逻辑来获取与出线相关的变量
        }

        execution.setVariable("f1",false);
        execution.setVariable("f2",true);
        log.info("[execute][execution({}) 被调用！变量有：{}]", execution.getId(),
                execution.getCurrentFlowableListener().getFieldExtensions());
    }

}